package com.gwt.tirso.gymkhana.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.gwt.tirso.gymkhana.shared.DelistedException;
import com.gwt.tirso.gymkhana.shared.FieldVerifier;

public class Login implements EntryPoint {

	private TextBox userText = new TextBox();
	private TextBox passwordText = new PasswordTextBox();
	private Button acceptButton = new Button();
	private Label userLabel = new Label();
	private Label passwordLabel = new Label();
	private Label errorLabel = new Label();
	private VerticalPanel mainPanel = new VerticalPanel();

	private String user = null;
	private String password = null;

	private ILoginServiceAsync loginService;

	@Override
	public void onModuleLoad() {
		
		loginService = ConnectionRpc.initRpcLogin();
		loginService.loginFromSessionServer(new AsyncCallback<Person>() {

			@Override
			public void onFailure(Throwable caught) {
				String details = caught.getMessage();
		        if (caught instanceof DelistedException) {
		          details = "Company '" + ((DelistedException)caught).getSymbol() + "' was delisted";
		        }

		        errorLabel.setText("Error: " + details);
		        errorLabel.setVisible(true);
				
			}

			@Override
			public void onSuccess(Person result) {
				if(result != null){
					RootPanel.get("login_widget").clear();
					RootPanel.get("login_widget").add(
							new LoginMainUI(result.getName()));

					mainPanel.addStyleName("login");
				}
			}
		});

		// Labels
		userLabel.setText("E-mail");
		passwordLabel.setText("Contraseña");
		

		// Button
		acceptButton.setText("Acceder");
		acceptButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				// method which receive the textBoxes info
				receiveInfo();
				if (user == null || password == null) {
					errorLabel.setText("Introduzca un usuario y contraseña");
					mainPanel.add(errorLabel);
				} else {

					loginService = ConnectionRpc.initRpcLogin();

					loginService.login(user, password,
							new AsyncCallback<Person>() {

								@Override
								public void onFailure(Throwable caught) {
									String details = caught.getMessage();
							        if (caught instanceof DelistedException) {
							          details = "Company '" + ((DelistedException)caught).getSymbol() + "' was delisted";
							        }

							        errorLabel.setText("Error: " + details);
							        errorLabel.setVisible(true);
								}

								@Override
								public void onSuccess(Person result) {
									if(result == null){
										Window.alert("Compruebe su email o su contraseña");
										
									}else{
										Window.alert("Conectado correctamente");
										RootPanel.get("login_widget").clear();
										RootPanel.get("login_widget").add(
												new LoginMainUI(result.getName()));

										mainPanel.addStyleName("login");
									}
									
								}
							});
				}

			}
		});

		// Adding elements to mainPanel
		mainPanel.add(userLabel);
		mainPanel.add(userText);
		mainPanel.add(passwordLabel);
		mainPanel.add(passwordText);
		mainPanel.add(acceptButton);
		
		//name the style, it is in CSS sheet
		mainPanel.addStyleName("login");

		// Adding mainPanel to RootPanel with the html-div name
		RootPanel.get("login_widget").add(mainPanel);
		

	}

	protected boolean verifiedValues(String user2, String password2) {
		// TODO Auto-generated method stub
		return false;
	}

	protected void receiveInfo() {
		String userAux = userText.getText();
		String passAux = passwordText.getText();

		if (!FieldVerifier.isValidName(userAux)
				|| !FieldVerifier.isValidName(passAux)) {
			return;
		} else {
			user = userAux;
			password = passAux;
		}
		return;
	}
	
	public void onModuleDisabled(){
		RootPanel.get("login_widget").clear();
	}
}
